/**
 * Created by Joseph on 2014/5/20.
 */
$(document).ready(function () {
    initClickable();
});

//封装所有的div显示和隐藏函数
function initClickable() {
    var addSelector = ".add-commodity input, .add-commodity select, .add-commodity textarea";
    var editSelector = ".edit-commodity input, .edit-commodity select, .edit-commodity textarea";

    //新增幻灯片
    $("#addCommodity").click(function () {
        showAddCommodity();
    });

    //新增幻灯片的返回按钮
    $("#add-return, #edit-return").click(function () {
        showAllCommodities();
    });

    //幻灯片的编辑按钮
    $(".commodity-display .update").click(function () {
        showEditCommodity();
    });

    //提交按钮
    $("#add-ok, #edit-ok").click(function(){
        var currentSelector;
        var falg = true;

        var currentId = $(this).attr("id");
         if(currentId == "add-ok")
            currentSelector = addSelector;
        else
            currentSelector = editSelector;

        $(currentSelector).each(function(index){
            var value = $(this).val();
            if( value == undefined|| value == null||value==""){
                flag = false;
            }
        });
        if(!flag){
            event.preventDefault();
            alert("请出入完整的信息");
        }
    });

    //重置按钮
    $("#add-reset, #edit-reset").click(function(){
        var currentSelector;
        var falg = true;

        var currentId = $(this).attr("id");
        if(currentId == "add-reset")
            currentSelector = addSelector;
        else
            currentSelector = editSelector;

        $(currentSelector).each(function(index){
             $(this).val("");
        });
    });
}

//显示所有幻灯片
function showAllCommodities() {
    $(".commodity-display").show();
    $(".edit-commodity").hide();
    $(".add-commodity").hide();
}
//显示添加幻灯片
function showAddCommodity() {
    $(".commodity-display").hide();
    $(".edit-commodity").hide();
    $(".add-commodity").show();
}
//显示编辑幻灯片
function showEditCommodity() {
    $(".commodity-display").hide();
    $(".edit-commodity").show();
    $(".add-commodity").hide();
}

